const fs = require('fs');
const path = require('path');

// 获取环境变量，默认为 'dev'
const buildEnv = process.env.BUILD_ENV || 'dev';

console.log(`Building for environment: ${buildEnv}`);

// 创建 dist 目录
const distDir = path.join(__dirname, 'dist');
if (!fs.existsSync(distDir)) {
    fs.mkdirSync(distDir, { recursive: true });
    console.log('Created dist directory');
}

// 复制 public 目录下的所有文件到 dist 目录
const publicDir = path.join(__dirname, 'public');
if (fs.existsSync(publicDir)) {
    const files = fs.readdirSync(publicDir);
    files.forEach(file => {
        const srcPath = path.join(publicDir, file);
        const destPath = path.join(distDir, file);
        
        if (fs.statSync(srcPath).isFile()) {
            fs.copyFileSync(srcPath, destPath);
            console.log(`Copied ${file} to dist/`);
        }
    });
    console.log(`Build completed successfully for ${buildEnv} environment`);
} else {
    console.error('Public directory not found');
    process.exit(1);
} 